// export const URL = 'http://101.91.221.35:8088/api' // 测试环境
// export const URL = 'https://www.shlxkj.info:9050/ARC1/api' // 测试环境
// export const URL = 'https://www.shlxkj.info/API/api' // 正式环境
// export const URL = 'https://www.shlxkj.info/TESTAPI/api'
export const URL = 'https://jinan.shlxkj.info:8010/api' // 济南环境 
import store from '@/store'
const params = (url, data) => {
	// if (store.state.vx_uid) {
	// 	data.uid = store.state.vx_uid
	// }
	// if (store.state.vx_token) {
	// 	data.token = store.state.vx_token
	// }
	url += '?'
	const keys = Object.keys(data)
	for (const key of keys) {
		url += `${key}=${encodeURIComponent(typeof data[key] === 'string' ? data[key].trim() : data[key])}&`
	}
	return url.substring(0, url.length - 1)
}

const request = ({ method, url, data = {} }) => {
	return new Promise((resolve, reject) => {
		console.log('url: ', params(url, data))
 		console.log('token', store.state.vx_token)
		uni.request({
			url: method === 'GET' ? params(url, data) : url,
			method,
			data,
			header: {
				Authorization: 'Bearer ' + store.state.vx_token,
				ApiKey: store.state.vx_token
			},
			success: (res) => {
				if (res.statusCode === 200) {
					console.log(res.data)
					if (res.data.Code !== 200) {
						let pages = getCurrentPages()
						let current = pages[pages.length - 1]
						if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
							return resolve(res.data)
						}
						reject(res.data)
					}
					resolve(res.data ? res.data : res)
				} else if (res.statusCode === 401) {
					store.commit('$uStore', { name: 'vx_token', value: '' })
					let pages = getCurrentPages()
					let current = pages[pages.length - 1]
					if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
						return resolve(res.data)
					}
					uni.showToast({ title: '需重新登录' })
					uni.navigateTo({ url: '/pages/login/index' })
				} else {
					uni.showToast({ icon: 'loading', title: '错误' })
					reject(res.data)
				}
				const data = res.data

				if (data) {
					resolve(data)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				if (err.statusCode === 401) {
					uni.redirectTo({ url: '/pages/login/index' })
				} else if (err.statusCode === 500) {
					uni.showToast({ title: '服务器错误' })
				} else {
					// #ifdef APP-PLUS
					// uni.getNetworkType({
					// 	success: function (res) {
					// 		console.log(res.networkType); //网络类型 wifi、2g、3g、4g、ethernet、unknown、none
					// 		if(res.networkType === "none"){
					// 			console.log("当前无网络");
					// 			uni.showModal({
					// 				title: '提示',
					// 				content: '请开启手机网络！',
					// 				showCancel: false,
					// 				success: (res) => {
					// 					if (res.confirm) {
					// 					console.log('用户点击了确定')
					// 					}
					// 				}
					// 			})
					// 		}else{
					// 			console.log("有网络");
					// 		}
					// 	}
					// })
					// #endif
				}
				console.log('err', err)
				reject(err)
			}
		})
	})
}

// 登录
export function login (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/GetLogin`,
		data
	})
}

// 获取手机号码
export function getPhone (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/decryptData`,
		data
		}
	)
}

// 注册
export function register (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/Login`,
		data
	})
}

// 用户协议
export function getDictionaryByType (data) {
	return request({
		method: 'GET',
		url: `${URL}/Dictionary/GetDictionaryByType`,
		data
	})
}

// 解密
export function AESDecrypt (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESDecrypt`,
		data
	})
}

// 加密
export function AESEncrypt (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESEncrypt`,
		data
	})
}

// 上报
export function CreateReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/CreateReagentResult`,
		data
	})
}

// 援助
export function UpdateReagentResult (data) {
	return request({
		method: 'PUT',
		url: `${URL}/ReagentResult/UpdateReagentResult`,
		data
	})
}

// 获取省市区
export function ConvertCoordinate (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/ConvertCoordinate`,
		data
	})
}

// 获取疾控中心
export function GetCDCVicinity (data) {
	return request({
		method: 'GET',
		url: `${URL}/CDC/GetCDCVicinity`,
		data
	})
}

// 报告过程数据
export function ReportedData (data) {
	return request({
		method: 'POST',
		url: `${URL}/WeChat/ReportedData`,
		data
	})
}

// 一键登录
export function AppLogin (data) {
	return request({
		method: 'GET',
		url: `${URL}/App/Login`,
		data
	})
}

// 扫码领取电子检测盒
export function Shipment (data) {
	return request({
		method: 'GET',
		url: `${URL}/SelfService/Shipment`,
		data
	})
}

// 获取版本号
export function GetDictionaryByType (data) {
	return request({
		method: 'GET',
		url: `${URL}/Dictionary/GetDictionaryByType`,
		data
	})
}

//Base64解码
export function Base64 (data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptMac`,
		data
	})
}

//查询是否存在关联
export function GetReleted(data) {
	return request({
		method: 'GET',
		url: `${URL}/MacPhone/GetReleted`,
		data
	})
}

//新建手机号与设备号关联
export function CreateReleted(data) {
	return request({
		method: 'POST',
		url: `${URL}/MacPhone/CreateReleted`,
		data
	})
}

//9006解密
export function aesDecrypt2(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptYuzhi`,
		data
	})
}
//9005解密结果值
export function aesDecryptjieguozhi(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptJieguozhi`,
		data
	})
}

//二次上报（结果）
export function UpReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/UpReagentResult`,
		data
	})
}

// 阈值值
// export function aesDecryptYuzhi() {
// 	return request({
// 		method: 'GET',
// 		url: `${URL}/Decrypt/getYuzhi`
// 	})
// }
//获取接口阈值
export function aesDecryptGetAPiyuzhi(data) {
	return request({
		method: 'GET',  
		url: `${URL}/Devicenoyuzhi/getYuzhiByReagentno`,  
		data
	})
}

//UDI码匹配
export function UDI(data) {
	return request({ 
		method: 'POST',
		url: `${URL}/Devicenoyuzhi/FindReagentnoyuzhi`,
		data
	})
}